package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.service.SaleListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("saleListGoods")
public class SaleListController {
    @Autowired
    private SaleListService saleListService;

    //1.2、销售单保存 和 对应销售商品
    @PostMapping("save")
    public ServiceVO saveSaleListAndGoods(SaleList saleList, String saleListGoodsStr, HttpServletRequest request){
        String saleNumber = request.getParameter("saleNumber");
        ServiceVO serviceVO = saleListService.saveSaleListAndGoods(saleList,saleListGoodsStr,saleNumber);
        return serviceVO;
    }
    //3.1、销售单查询（可查询条件：销售单号、客户、付款状态）
    @PostMapping("list")
    public Map<String,Object> getSaleList(String saleNumber, Integer customerId, Integer state, String sTime,String eTime){
        List<SaleList> list = saleListService.getSaleList(saleNumber,customerId,state,sTime,eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }
    //3.2、销售单商品信息
    @PostMapping("goodsList")
    public Map<String,Object> getSaleListGoodsList(Integer saleListId){
       List<SaleListGoods> list = saleListService.getSaleListGoodsList(saleListId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }
    //3.3、删除销售单
    @PostMapping("delete")
    public ServiceVO deleteSaleListAndGoods(Integer saleListId){
        ServiceVO serviceVO = saleListService.deleteSaleListAndGoods(saleListId);
        return serviceVO;
    }
    //2.1、支付结算（修改销售单付款状态）
    @PostMapping("updateState")
    public ServiceVO updateStateSaleList(Integer saleListId){
        ServiceVO serviceVO = saleListService.updateStateSaleList(saleListId);
        return serviceVO;
    }
    //4.1、销售统计（可根据 商品类别、商品编码或名称 条件查询）
    @PostMapping("count")
    public String countSaleListGoods(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        String jsonStr = saleListService.countSaleListGoods(sTime,eTime,goodsTypeId,codeOrName);
        return jsonStr;
    }
    //5.1、按日统计接口
    @PostMapping("getSaleDataByDay")
    public String getSaleDataByDay(String sTime, String eTime){
        String jsonStr = saleListService.getSaleDataByDay(sTime,eTime);
        return jsonStr;
    }
    //6.1、按月统计接口
    @PostMapping("getSaleDataByMonth")
    public String getSaleDataByMonth(String sTime, String eTime){
        String jsonStr = saleListService.getSaleDataByMonth(sTime,eTime);
        return jsonStr;
    }
}
